using System;
using System.Web;
using System.Web.UI.WebControls;
using System.Xml;
using FormsGenerator.Xml;

namespace FormsGenerator
{
	/// <summary>
	/// Summary description for ViewSource.
	/// </summary>
	public partial class CellProperties : System.Web.UI.Page
	{

		protected XmlDocument formXmlDocument;
		protected XmlElement cellElement;
		protected string position;
		protected int pagenumber;

		protected void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here

			if (Request["id"] == null || Request["id"].Equals(""))
			{
				//Use Server.Transfer to support Reverse proxy. Do not use Response.Redirect!
				Server.Transfer("default.aspx");
			}
			else
			{
				ViewState["GUID"] = Request["id"].Trim();
			}

			if (Request["position"] != null && !Request["position"].Equals(""))
			{
				position = Request["position"];
				ViewState["position"] = position;
			}
			else
			{
				position = (string) ViewState["position"];
			}

			if (Request["page"] != null && !Request["page"].Equals(""))
			{
				pagenumber = int.Parse(Request["page"]);
				ViewState["page"] = pagenumber;
			}
			else
			{
				pagenumber = int.Parse((string) ViewState["page"]);
			}

			try
			{
				formXmlDocument = XmlHandler.GetForm((string) ViewState["GUID"]);
				cellElement = XmlHandler.GetCell(formXmlDocument, pagenumber, position);
			}
			catch (Exception ex)
			{
				LabelErrorMessage.Visible = true;
				LabelErrorMessage.Text += ex.Message;
			}
			if (!IsPostBack)
			{
				BindData();

			}
		}

		private void BindData()
		{
			if (cellElement != null)
			{
				string[] positionArray = position.Split(new Char[] {'_'});
				LabelPosition.Text = "row: " + positionArray[0] + ", column: " + positionArray[1];

				//Colspan
				string colSpan = cellElement.GetAttribute("colspan");
				TextBoxColSpan.Text = colSpan;
				//Rowspan
				string rowSpan = cellElement.GetAttribute("rowspan");
				TextBoxRowSpan.Text = rowSpan;

				//Alignment
				DropDownListAlignment.Items.Add(new ListItem("None", ""));
				DropDownListAlignment.Items.Add(new ListItem("Left", "left"));
				DropDownListAlignment.Items.Add(new ListItem("Center", "center"));
				DropDownListAlignment.Items.Add(new ListItem("Right", "right"));
				string alignment = cellElement.GetAttribute("align");
				if (alignment != null || !alignment.Trim().Equals(""))
				{
					DropDownListAlignment.Items.FindByValue(alignment.ToLower()).Selected = true;
				}
			}
		}

		#region Web Form Designer generated code

		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}

		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{

		}

		#endregion

		protected void ButtonUpdate_Click(object sender, System.EventArgs e)
		{
			try
			{
				LabelExecuteJavascript.Text = "<script>updateForm('" + (string) ViewState["GUID"] + "', false);</script>";
				updateCell();
			}
			catch (Exception ex)
			{
				LabelErrorMessage.Visible = true;
				LabelErrorMessage.Text += ex.Message;
			}
		}

		protected void ButtonUpdateAndClose_Click(object sender, System.EventArgs e)
		{
			LabelExecuteJavascript.Text = "<script>updateForm('" + (string) ViewState["GUID"] + "', true);</script>";
			updateCell();
		}

		private void updateCell()
		{
			try
			{
				//Colspan
				int colSpan = 1;
				if (TextBoxColSpan.Text.Trim().Length > 0)
				{
					colSpan = int.Parse(TextBoxColSpan.Text);
				}
				if (colSpan > 1)
				{
					XmlAttribute colspanAttribute = cellElement.GetAttributeNode("colspan");
					if (colspanAttribute == null)
					{
						colspanAttribute = formXmlDocument.CreateAttribute("colspan");
						cellElement.Attributes.Append(colspanAttribute);
					}
					colspanAttribute.Value = TextBoxColSpan.Text;
				}
				else
				{
					cellElement.RemoveAttribute("colspan");
				}

				//Rowspan
				int rowSpan = 1;
				if (TextBoxRowSpan.Text.Trim().Length > 0)
				{
					rowSpan = int.Parse(TextBoxRowSpan.Text);
				}
				if (rowSpan > 1)
				{
					XmlAttribute rowspanAttribute = cellElement.GetAttributeNode("rowspan");
					if (rowspanAttribute == null)
					{
						rowspanAttribute = formXmlDocument.CreateAttribute("rowspan");
						cellElement.Attributes.Append(rowspanAttribute);
					}
					rowspanAttribute.Value = TextBoxRowSpan.Text;
				}
				else
				{
					cellElement.RemoveAttribute("rowspan");
				}

				//Alignment
				string alignment = DropDownListAlignment.SelectedItem.Value;
				if (alignment.Length > 0)
				{
					XmlAttribute alignAttribute = cellElement.GetAttributeNode("align");
					if (alignAttribute == null)
					{
						alignAttribute = formXmlDocument.CreateAttribute("align");
						cellElement.Attributes.Append(alignAttribute);
					}
					alignAttribute.Value = alignment;
				}
				else
				{
					cellElement.RemoveAttribute("align");
				}
				XmlHandler.SaveForm((string) ViewState["GUID"], formXmlDocument, HttpContext.Current.User.Identity.Name);
				LabelExecuteJavascript.Visible = true;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
	}
}